<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理系统登录页面</title>
    <link rel="shortcut icon" href="../images/book.png">
    <!-- 引入element-ui -->
    <link rel="stylesheet" href="../plugins/element-ui/index.css"/>
    <!-- 引入一些小图标组件 -->
    <link rel="stylesheet" href="../styles/icon/iconfont.css" />
    <!-- login页面CSS -->
    <link rel="stylesheet" href="../styles/login.css">
    <style>
        html{
            height: 100%;
        }

        body{
            height: 100%;
            width: 100%;
            margin: 0;
        }
    </style>
</head>

<body>
<div class="login" id="login-app">
    <div class="login-box">
        <!-- 左侧图片 -->
        <img src="../images/login.jpg" alt="">
        <!-- 登录表单 -->
        <div class="login-form">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
                <!-- 用户名框 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" type="text"
                              auto-complete="off" placeholder="账号" maxlength="20"
                              prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <!-- 密码框 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" placeholder="密码"
                              prefix-icon="iconfont icon-lock" maxlength="20"
                              @keyup.enter.native="handleLogin"></el-input>
                </el-form-item>
                <!-- 登录按钮 -->
                <el-form-item style="width:100%;">
                    <el-button :loading="loading" class="login-btn" size="medium"
                               type="primary" style="width:100%;"
                               @click.native.prevent="handleLogin">
                        <span v-if="!loading">登录</span>
                        <span v-else>登录中...</span>
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</div>

<!-- vue开发环境版本，包含了有帮助的命令行警告 -->
<script src="../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../plugins/axios/axios.min.js"></script>
<!-- 引入自己写的VUE-AXIOS 请求响应拦截器 -->
<script src="../js/request.js"></script>
<!-- 发送请求API -->
<script src="../api/login.js"></script>

<script>
    new Vue({
        el: '#login-app',
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                loading: false
            }
        },
        computed: {
            // 登录规则
            loginRules() {
                // 确认用户名
                const validateUsername = (rule, value, callback) => {
                    if (value.length < 1) {
                        callback(new Error('请输入用户名'))
                    } else {
                        callback()
                    }
                }
                // 确认密码
                const validatePassword = (rule, value, callback) => {
                    if (value.length < 6) {
                        callback(new Error('密码必须在6位以上'))
                    } else {
                        callback()
                    }
                }
                // 登录规则
                return {
                    'username': [{'validator': validateUsername, 'trigger': 'blur'}],
                    'password': [{'validator': validatePassword, 'trigger': 'blur'}]
                }
            }
        },
        created() {
        },
        methods: {
            // 处理登录函数
            async handleLogin() {
                this.$refs.loginForm.validate(async (valid) => {
                    if (valid) {
                        // 切换为正在登录状态
                        this.loading = true
                        // 调用登录API发送请求
                        let res = await loginApi(this.loginForm)
                        // 如果后端查询数据库成功 那么登录成功 将该用户信息存储localStorage 并重定向index.html
                        if (String(res.code) === '1') {
                            localStorage.setItem('userInfo', JSON.stringify(res.data))
                            window.location.href = '/index.html'
                        } else {
                            // 登录不成功
                            this.$message.error(res.msg)
                            this.loading = false
                        }
                    }
                })
            }
        }
    })
</script>
</body>
</html>
